package com.xms.organization.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xms.datacore.SqlDescriptor;
import com.xms.datacore.provider.IDataRepositoryBase;
import com.xms.organization.data.BusinessunitMapper;
import com.xms.organization.entity.Businessunit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author migo
 * @since 2021-08-27
 */
@Service
public class BusinessunitService extends ServiceImpl<BusinessunitMapper, Businessunit> implements IBusinessUnitService {
    private IDataRepositoryBase _dataRepositoryBase;
    @Autowired
    public BusinessunitService(IDataRepositoryBase dataRepositoryBase){
        _dataRepositoryBase = dataRepositoryBase;
    }

    @Override
    public boolean isChild(UUID parentId, UUID businessUnitId) throws SQLException {
        SqlDescriptor sql = new SqlDescriptor("SELECT 1 FROM (SELECT businessunitid FROM ufn_org_getdepttree(?)) AS B WHERE businessunitid = ?", parentId, businessUnitId);

        return _dataRepositoryBase.exists(sql);
    }
}
